//
//  BMMain.m
//  IntelligentSunglasses
//
//  Created by bosma on 14-4-24.
//  Copyright (c) 2014年 cn.com.bosma. All rights reserved.
//

#import "BMMain.h"
#import "LeftView.h"
#import "RightView.h"
#import "FrontView.h"
#import "BMSliderPageController.h"

@interface BMMain ()<SliderPageControlDelegate, UIScrollViewDelegate>

@end

@implementation BMMain

@synthesize scrollView;
@synthesize demoContent;
@synthesize sliderPageControl;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title =@"首页";
}


- (id)init
{
	if (self = [super init])
	{
        //页数的数组
        self.demoContent = [NSMutableArray new];
		NSMutableDictionary *pageView1 = [NSMutableDictionary dictionary];
		[self.demoContent addObject:pageView1];
		NSMutableDictionary *pageView2 = [NSMutableDictionary dictionary];
		[self.demoContent addObject:pageView2];
		NSMutableDictionary *pageView3 = [NSMutableDictionary dictionary];
		[self.demoContent addObject:pageView3];

      
		//滑动视图
		self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
        [self.scrollView setBounces:NO];
		[self.scrollView setPagingEnabled:YES];
		[self.scrollView setContentSize:CGSizeMake([self.demoContent count]*self.scrollView.frame.size.width, self.scrollView.frame.size.height-64)];
        
		[self.scrollView setShowsHorizontalScrollIndicator:NO];
        [self.scrollView setShowsVerticalScrollIndicator:NO];
		[self.scrollView setDelegate:self];
		[self.view addSubview:self.scrollView];
        
		//标签条
		self.sliderPageControl = [[BMSliderPageController  alloc] initWithFrame:CGRectMake(85,553,150,15)];
		[self.sliderPageControl addTarget:self action:@selector(onPageChanged:) forControlEvents:UIControlEventValueChanged];
        [self.sliderPageControl setUserInteractionEnabled:NO];
		[self.sliderPageControl setDelegate:self];
		[self.sliderPageControl setShowsHint:YES];
		[self.view addSubview:self.sliderPageControl];
		[self.sliderPageControl setNumberOfPages:[self.demoContent count]];
		[self.sliderPageControl setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
        
        //主页 ->左侧视图
        LeftView *left = [[LeftView alloc]initWithFrame:self.view.frame];
        [scrollView addSubview:left];
        
        //主页 ->视图
        FrontView*front = [[FrontView alloc]initWithFrame:CGRectMake(320,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height)];
        [scrollView addSubview:front];
        
        //主页 ->右侧视图
        RightView *right = [[RightView alloc]initWithFrame:CGRectMake(640,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height)];
        [scrollView addSubview:right];

		//初始化选中标签
		[self changeToPage:1 animated:YES];
	}
	return self;
}


- (void) xxxxxxx:(id)sender
{
    NSLog(@"xxx");
}


#pragma mark scrollview delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView_
{
	pageControlUsed = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView_
{
    if (pageControlUsed)
	{
        return;
    }
	
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	
	[sliderPageControl setCurrentPage:page animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView_
{
	pageControlUsed = NO;
}


#pragma mark sliderPageControlDelegate

- (NSString *)sliderPageController:(id)controller hintTitleForPage:(NSInteger)page
{
	NSString *hintTitle = [[self.demoContent objectAtIndex:page] objectForKey:@"title"];
	return hintTitle;
}

//是否改变页面
- (void)onPageChanged:(id)sender
{
	pageControlUsed = YES;
	[self slideToCurrentPage:YES];
	
}


//改变页面
- (void)changeToPage:(int)page animated:(BOOL)animated
{
	[sliderPageControl setCurrentPage:page animated:YES];
	[self slideToCurrentPage:animated];
}

//滑动至最近页面
- (void)slideToCurrentPage:(bool)animated
{
	int page = sliderPageControl.currentPage;
	
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
